#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) The user_data Project.
# See LICENSE.txt for details.
import types
import kernel.base.tmp_user_data as user
import kernel.base.function as func
import copy

#import os

#    user={}  #放置一些配置这里不在是用户数据拉比如 禁言Gag，用户名User name，昵称Nickname,用户ID user_id，工会Union，地图ID，角色坐标,socket句柄fp。。。。
#    user["status"] 0死亡 1或者 2战斗中  user["team"] 0个体 》0队伍ID（通常是时间撮）
#    phpfp=False
    
#    def __init__(self):
#        global user
#        self.user = user
        
#添加php的句柄
def addphpamf(fd):
    user.phpfp=fd
 
#取得php的句柄   
def getphpamf():
    return user.phpfp

#开启游戏
def startgame():
    user.gamestate=True
    
#取得游戏状态
def getgame():
    return user.gamestate

#停止游戏
def stopgame():
    user.gamestate=False

#判断用户是否在线
def online(user_id):
    if(type(user.user)==dict):
        if(user_id in user.user):
            return True
        else:
            return False
        
#用户注册
def user_reg(user_id,fp):
    user.user[user_id]={"user_id":user_id,"fp":fp,"ip":func.ip2int(fp.getPeer().host),"login":0}
        
#更新注册信息

#取得所有用户列表
def get_user_list():
    if(type(user.user)==dict):
        return user.user
    return False

#取得这个地图中所有在线的用户
def get_map_user(map_id,line=1):
    mapuser={}
    i=0
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["map_id"]==map_id and user.user[item]["line"]==line):
                mapuser[i]=user.user[item]
                i=i+1
    return mapuser

#用户ID取得用户数据
def get_user(user_id,key=""):
    if(online(user_id)):
        if(key!=""):
            return user.user[user_id][key]
        else:
            return user.user[user_id]
    return False
        
#昵称取得用户信息
def nntoinfo(nickname):
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["nickname"]==nickname):
                return user.user[item]
    return False

#取得某帮会成员id列表
def unionlist(union_id):
    unionlist=[]
    i=0
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["union_id"]==union_id):
                unionlist[i]=user.user[item]["user_id"]
                i+=1
    return unionlist

#根据用户ID取得句柄
def get_fp(user_id):
    if(type(user.user)==dict):
        if(user.user.get(user_id)):
            return user.user[user_id]["fp"]
    return False

#根据句柄取得用户ID
def get_userid(fp):
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["fp"]==fp):
                return user.user[item]["user_id"]
    return False

#根据IP查询用户数据
def get_userip(ip):
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["ip"]==ip):
                return user.user[item]
    return False

#根据用户删除用户
def del_user(user_id):
    if(type(user.user)==dict):
        if(user.user.get(user_id)):
            del user.user[user_id]
            
#根据句柄删除用户
def del_user_fp(fp):
    if(type(user.user)==dict):
        delkey=0
        for item in user.user:
            if(user.user[item]["fp"]==fp):
                delkey=item
                break
        if delkey:
            del user.user[item]

#移动用户数据,就是从新创建新的用户ID
def move_user(olb_user_id,new_user_id):
    if(type(user.user)==dict):
        if(olb_user_id in user.user):
            user.user[new_user_id]=copy.deepcopy(user.user[olb_user_id])
            user.user[olb_user_id]["login"]=1
            del user.user[olb_user_id]
        
#取出组队用户列表
def team_list(team_id):
    team_list={}
    i=0
    if(type(user.user)==dict):
        for item in user.user:
            if(user.user[item]["team"]==team_id):
                team_list[i]=user.user[item]
                i+=1
                
    if(team_list):
        return team_list
    else:
        return False

#打印出用户列表 
def printuser():
    if(type(user.user)==dict):
        for item in user.user:
            print(user.user[item]["user_id"])
            
#用户属性调整
def user_property(user_id,propertykey,propertyvalue):
    if(type(user.user)==dict):
        user.user[user_id][propertykey]=propertyvalue
        
#构建普通协议
def agreement(module,fun,returns,parameter=[],typess="return"):
    return {"sys":False,"module":module,"fun":fun,"type":typess,"return":returns,"parameter":parameter}

#构建系统协议 showtype  chat条理部   top 顶部 right右下角  down通知下线  email新邮件通知
def sysagreement(returns,showtype="chat"):
    returns["res"]=False
    returns["showtype"]=showtype
    return {"sys":True,"type":"return","return":returns}

#过滤关键字载入
def keyfilters_load(mongo):
    c_key=mongo.find("c_key",{},{"key":True,"replace":True})
    if(c_key):
        i=0
        for keyse in c_key:
            user.keyfilters[i]={"key":keyse["key"],"replace":keyse["replace"]}
            i=i+1
            
#取得关键字列表
def getkeylist():
    return user.keyfilters

#设置存储
def setstorage(storage):
    user.storage=storage
    
#读取存储
def storage():
    return user.storage

#获取文件上次执行时间
def get_file_time(classmd5):
    if func.is_arr(user.file_time, classmd5):
        return user.file_time[classmd5]
    else:
        return 0

#设置时间
def set_file_time(classmd5,times):
    user.file_time[classmd5]=times
    